// 全局请求封装
const base_url = 'https://vote.api.tzcpa.vip'
// const base_url = 'http://sqjsapi.zimaoyun.cn'
// var base_url = process.env.NODE_ENV === 'development' ? 'http://localhost:8080/devapi' : 'http://localhost:8080/devapi'
//这个基本地址可替换成你需要的地址
 
// 请求超出时间
const timeout = 5000
 
// 需要修改token，和根据实际修改请求头
export default (params) => {
	let url = params.url;
	let method = params.method || "get";
	let data = params.data || {};
	let header = {
		// 'Blade-Auth': uni.getStorageSync('token') || '',
		// 'Content-Type': 'application/json;charset=UTF-8',
		// 'Authorization': 'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
		// 'Tenant-Id': uni.getStorageSync('tenantId') || 'xxx', // avue配置相关
		// ...params.header
		'Authorization':uni.getStorageSync('token')
	}
	if (method == "post") {
		// header = {
		// 	'Content-Type': 'application/json'
		// };
		// header.Content-Type 
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url:base_url+ url,
			method: method,
			header: header,
			data: data,
			timeout,
			success(response) {
				console.log(response)
				const res = response
				// 根据返回的状态码做出对应的操作
				//获取成功
				// console.log(res.statusCode);
				if (res.statusCode == 200) {
					if(res.data.code == 401){
						// 登录失效
						uni.removeStorageSync('token');
						debugger
						// uni.showToast({
						// 	title:'登录失效，请重新登录',
						// 	icon:"error",
						// 	success: () => {
						// 		setTimeout(()=>{
						// 			uni.showTabBar({
						// 				url:'/pages/index/index'
						// 			})
						// 		},1500)
						// 	}
						// })
						uni.showModal({
							title: "提示",
							content: "登录超时，请重新登录",
							showCancel: false,
							success:()=> {
								let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
								let curRoute = routes[routes.length - 1].route // 获取当前页面路由，也就是最后一个打开的页面路由
								console.log(curRoute);
								if(curRoute == 'pages/index/index'){
									location.reload();
								}else{
									let origin = window.location.origin;
									console.log(origin)
									let url = origin+'/#/pages/index/index';
									console.log(url)
									location.href = url
								}
							},
						});
					}else{
						resolve(res.data);
					}
				} else {
					uni.clearStorageSync()
					switch (res.statusCode) {
						case 401:
							uni.showModal({
								title: "提示",
								content: "请登录",
								showCancel: false,
								success() {
									// setTimeout(() => {
									// 	uni.navigateTo({
									// 		url: "/pages/login/index",
									// 	})
									// }, 1000);
								},
							});
							break;
						case 404:
							uni.showToast({
								title: '请求地址不存在...',
								duration: 2000,
							})
							break;
						default:
							uni.showToast({
								title: '请重试...',
								duration: 2000,
							})
							break;
					}
				}
			},
			fail(err) {
				console.log(err)
				if (err.errMsg.indexOf('request:fail') !== -1) {
					uni.showToast({
						title: '网络异常',
						icon: "error",
						duration: 2000
					})
				} else {
					uni.showToast({
						title: '未知异常',
						duration: 2000
					})
				}
				reject(err);
 
			},
			complete() {
				// 不管成功还是失败都会执行
				uni.hideLoading();
				uni.hideToast();
			}
		});
	}).catch(() => {});
};